package org.example.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.checkerframework.checker.units.qual.A;
import org.example.config.R;
import org.example.domain.RuoYi;
import org.example.service.RuoYiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("ruoyi")
public class RuoYiController {
   @Autowired
    RuoYiService ruoYiService;
    @RequestMapping("ruoyiById")
    public R ruoyiById(Integer id){
        List<RuoYi> list = ruoYiService.ruoyiList(id);
        return R.successed(list);
    }
   @RequestMapping("list")
    public R list(@RequestBody RuoYi ruoYi, Page page){
       PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
       List<RuoYi> list=ruoYiService.list(ruoYi);
       PageInfo<RuoYi> ruoYiPageInfo = new PageInfo<>(list);
       return R.successed(ruoYiPageInfo);
   }

   @RequestMapping("updateById")
    public R updateById(Integer id){
       boolean b=ruoYiService.updateById(id);
       return R.successed(null);
   }

   @RequestMapping("update")
    public R update(@RequestBody RuoYi ruoYi){
      boolean b= ruoYiService.update(ruoYi);
      return R.successed(null);
   }

   @RequestMapping("add")
    public R add(@RequestBody RuoYi ruoYi){
      boolean b= ruoYiService.add(ruoYi);

      return R.successed(null);
   }

   @RequestMapping("delAlls")
    public R delAlls(Integer[] ids){
       for (Integer id : ids) {
           ruoYiService.updateById(id);
       }
       return R.successed(null);
   }

   @Autowired
    HttpServletResponse response;

   @RequestMapping("daochuCompany")
    public R daochu() throws IOException {
       ExcelWriter writer = ExcelUtil.getWriter(true);

       writer.addHeaderAlias("id","id");
       writer.addHeaderAlias("username","username");
       writer.addHeaderAlias("nickName","nickName");
       writer.addHeaderAlias("name","name");
       writer.addHeaderAlias("tel","tel");
       writer.addHeaderAlias("status","status");
       writer.addHeaderAlias("createTime","createTime");
       writer.addHeaderAlias("flag","flag");
       writer.addHeaderAlias("service","service");

       List<RuoYi> list=ruoYiService.lists();

       writer.write(list,true);

       response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
       response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
       ServletOutputStream out=response.getOutputStream();

       writer.flush(out, true);
       writer.close();
       IoUtil.close(out);
       return R.successed(null);
   }

   @RequestMapping("nongHuList")
    public R nongHuList(@RequestBody RuoYi ruoYi,Page page){
       PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
      List<RuoYi> list= ruoYiService.nonghuList(ruoYi);
       PageInfo<RuoYi> info = new PageInfo(list);
       return R.successed(info);
   }

    @RequestMapping("daochuNonghu")
    public R daochuNonghu() throws IOException {
        ExcelWriter writer = ExcelUtil.getWriter(true);

        writer.addHeaderAlias("id","id");
        writer.addHeaderAlias("username","username");
        writer.addHeaderAlias("nickName","nickName");
        writer.addHeaderAlias("name","name");
        writer.addHeaderAlias("tel","tel");
        writer.addHeaderAlias("status","status");
        writer.addHeaderAlias("createTime","createTime");
        writer.addHeaderAlias("flag","flag");
        writer.addHeaderAlias("service","service");

        List<RuoYi> list=ruoYiService.listNonghu();

        writer.write(list,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
        return R.successed(null);
    }




}
